package utils 
{
	import flash.geom.Rectangle;
	/**
	 * ...
	 * @author Jeroen Gildemacher
	 */
	public class RectangleHelper 
	{
		public static const OVERLAP_LEFT:String = "overlap left";
		public static const OVERLAP_RIGHT:String = "overlap right";
		public static const OVERLAP_MIDDLE:String = "overlap middle";
		public static const OVERLAP_NONE:String = "overlap none";
		
		
		public function RectangleHelper() 
		{
			
		}
		/**
		 * checks overlap and returns a const indicating type of overlap from the
		 * viewpoint of r2
		 * @param	r1
		 * @param	r2
		 * @return type of overlap, left/right, middle
		 */
		public static function hasOverlap(r1:Rectangle, r2:Rectangle):String {
			if ( r2.left> r1.right
					|| r2.right < r1.left
					|| r2.top > r1.bottom
					|| r2.bottom < r1.top) {
						return OVERLAP_NONE;
					}
			if (r2.left < r1.right && r2.left>r1.left ) {
				return OVERLAP_LEFT;
			}
			if (r2.left < r1.right && r2.left>r1.left ) {
				return OVERLAP_LEFT;
			}
			return OVERLAP_NONE;
		}
		
		/**
		 * Gives the overlap of 2 rectangles in the form of the 
		 * @param	r1
		 * @param	r2
		 * @return
		 */
		public static function getOverlapRectangle(r1:Rectangle, r2:Rectangle):Rectangle {
			var intersect:Rectangle = new Rectangle;
			intersect.left = Math.max(r1.left, r2.left);
			intersect.top = Math.max(r1.top, r2.top);
			intersect.right = Math.min(r1.right, r2.right );
			intersect.bottom = Math.min(r1.bottom, r2.bottom);	
			return intersect;
		}
		
		
	}

}